package com.example.speeddating.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.entity.Article;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.speeddating.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:33
 */
@Mapper
public interface ArticleMapper extends BaseMapper<Article> {

    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "topic_id",property = "topicId"),
            @Result(column = "topic_id",property = "topic",one = @One(select = "com.example.speeddating.mapper.TopicMapper.selectTopicById")),
            @Result(column = "id",property = "commentNum",one = @One(select = "com.example.speeddating.mapper.CommentMapper.selectArticleCommentNum")),
            @Result(column = "id",property = "thumpNum",one = @One(select = "com.example.speeddating.mapper.LikeMapper.selectArticleLikeNum")),
            @Result(column = "id",property = "urls",one = @One(select = "com.example.speeddating.mapper.UserMapper.selectUserAvatorList")),
    })
    @Select("SELECT a.id,a.user_id,a.content,a.location,a.create_time,a.topic_id,t.`name` talk,t.id from article a ,topic t WHERE t.id = a.topic_id and a.deleted=0 ORDER BY a.create_time DESC")
    Page<Article> selectArticle(Page<Article> page);


    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "topic_id",property = "topicId"),
            @Result(column = "topic_id",property = "topic",one = @One(select = "com.example.speeddating.mapper.TopicMapper.selectTopicById")),
            @Result(column = "id",property = "commentNum",one = @One(select = "com.example.speeddating.mapper.CommentMapper.selectArticleCommentNum")),
            @Result(column = "id",property = "thumpNum",one = @One(select = "com.example.speeddating.mapper.LikeMapper.selectArticleLikeNum")),
            @Result(column = "id",property = "urls",one = @One(select = "com.example.speeddating.mapper.UserMapper.selectUserAvatorList")),
    })
    @Select("SELECT a.id,a.user_id,a.content,a.location,a.create_time,a.topic_id,t.`name` talk,t.id from article a ,topic t WHERE t.id = a.topic_id and a.id = #{id}")
    Article selectArticleById(Long id);

    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "topic_id",property = "topicId"),
            @Result(column = "topic_id",property = "topic",one = @One(select = "com.example.speeddating.mapper.TopicMapper.selectTopicById")),
            @Result(column = "id",property = "commentNum",one = @One(select = "com.example.speeddating.mapper.CommentMapper.selectArticleCommentNum")),
            @Result(column = "id",property = "thumpNum",one = @One(select = "com.example.speeddating.mapper.LikeMapper.selectArticleLikeNum")),
            @Result(column = "id",property = "urls",one = @One(select = "com.example.speeddating.mapper.UserMapper.selectUserAvatorList")),
    })
    @Select("SELECT a.id,a.user_id,a.content,a.location,a.create_time,a.topic_id,t.`name` talk,t.id  from article a,topic t WHERE t.id = a.topic_id and a.deleted=0 and a.topic_id = #{topicId} ORDER BY a.create_time DESC")
    Page<Article> selectArticleByTopicId(Page<Article> page,long topicId);


    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "topic_id",property = "topicId"),
            @Result(column = "topic_id",property = "topic",one = @One(select = "com.example.speeddating.mapper.TopicMapper.selectTopicById")),
            @Result(column = "id",property = "commentNum",one = @One(select = "com.example.speeddating.mapper.CommentMapper.selectArticleCommentNum")),
            @Result(column = "id",property = "thumpNum",one = @One(select = "com.example.speeddating.mapper.LikeMapper.selectArticleLikeNum")),
            @Result(column = "id",property = "urls",one = @One(select = "com.example.speeddating.mapper.UserMapper.selectUserAvatorList")),
    })
    @Select("SELECT a.id,a.user_id,a.content,a.location,a.create_time,a.topic_id,t.`name` talk,t.id from article a,topic t WHERE t.id = a.topic_id and a.deleted=0 and a.user_id = #{userId} ORDER BY a.create_time DESC")
    Page<Article> selectArticleByUserId(Page<Article> page,long userId);


    /**
     * 关注列表的文章
     * @param userPage
     * @param userId
     * @return
     */
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "topic_id",property = "topicId"),
            @Result(column = "topic_id",property = "topic",one = @One(select = "com.example.speeddating.mapper.TopicMapper.selectTopicById")),
            @Result(column = "id",property = "commentNum",one = @One(select = "com.example.speeddating.mapper.CommentMapper.selectArticleCommentNum")),
            @Result(column = "id",property = "thumpNum",one = @One(select = "com.example.speeddating.mapper.LikeMapper.selectArticleLikeNum")),
            @Result(column = "id",property = "urls",one = @One(select = "com.example.speeddating.mapper.UserMapper.selectUserAvatorList")),
    })
    @Select(value = "SELECT a.id,a.user_id,a.content,a.location,a.create_time,a.topic_id,t.`name` talk,t.id FROM article a,topic t WHERE  t.id = a.topic_id and a.deleted=0 and  a.user_id IN (SELECT u.id FROM follow f,`user` u WHERE f.follow_id = u.id AND  f.user_id = #{userId}) ORDER BY a.create_time DESC")
    Page<Article> selectFollowArticleList(Page<Article> userPage, Long userId);

    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "topic_id",property = "topicId"),
            @Result(column = "topic_id",property = "topic",one = @One(select = "com.example.speeddating.mapper.TopicMapper.selectTopicById")),
            @Result(column = "id",property = "commentNum",one = @One(select = "com.example.speeddating.mapper.CommentMapper.selectArticleCommentNum")),
            @Result(column = "id",property = "thumpNum",one = @One(select = "com.example.speeddating.mapper.LikeMapper.selectArticleLikeNum")),
            @Result(column = "id",property = "urls",one = @One(select = "com.example.speeddating.mapper.UserMapper.selectUserAvatorList")),
    })
    @Select("SELECT a.id,a.user_id,a.content,a.location,a.create_time,a.topic_id,t.`name` talk,t.id from article a,topic t WHERE t.id = a.topic_id and a.deleted=0 and a.id = #{articleId}")
    Article selectArticleById(long articleId);

    @Select("SELECT count(*) FROM article WHERE DATE(create_time) = CURDATE() AND user_id = #{userId}")
    public int selectIsPublishInToday(long userId);

    @Select("SELECT count(*) FROM `article` WHERE topic_id =#{id}")
    public int selectTopicCount(long id);


    @Select(value = "select * from article where user_id = #{userId} and deleted = 0 ORDER BY create_time DESC")
    Page<Article> selectArticleListByUserId(Page<Article> articlePage,Long userId);

    @Select("SELECT image1 FROM article_image i,article a WHERE a.id = i.article_id AND a.id = #{articleId} AND a.deleted = 0")
    public String selectArticleFirstImage(Long articleId);

    @Select("SELECT * FROM `article` WHERE id in (SELECT article_id FROM slike WHERE user_id in #{ids} AND deleted = 0) AND deleted = 0")
    public List<Article> selectArticlesByUserLike(List<Long> ids);
}
